﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Reflection;
using System.Security;
using System.Text;

namespace PoshRabbit
{
    [Cmdlet(VerbsData.Publish, "RabbitMessage")]
    public class PublishMessageCmdlet : SharedConnectionRabbitCmdletBase
    {
        [Parameter( ValueFromPipeline = true, Mandatory = true)]
        [Alias( "value", "input" )]
        [ValidateNotNullOrEmpty]
        public string Message { get; set; }

        byte[] MessageBytes
        {
            get
            {
                return MessageEncoding.GetBytes(Message);
            }
        }

        protected override void ProcessRecord()
        {
            if( null == Model )
            {
                throw new InvalidOperationException( "the model is not available");
            }

            WriteDebug("publishing message '{0}' with encoding '{1}'".FormatWith(Message, Encoding));
            Model.BasicPublish( Exchange,RoutingKey,null, MessageBytes);
            WriteDebug( "publish complete");
        }
    }
}
